﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
using DigtiMatrix.Entity;
using DigitMatrix.DataAccess;
using log4net;
using System.Drawing;


namespace DigitMatrix.Business
{
    public class CameraBusiness
    {
        public static readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private static CameraBusiness instance;

        public static CameraBusiness Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new CameraBusiness();
                }
                return instance;
            }
        }
        public List<SysEvent> SearchSysLog(int eventType, int camercaId, DateTime dStartTime, DateTime dEndDate, ref string errMessage)
        {
            Database db = DatabaseFactory.CreateDatabase();
            string baseOutpputPath = Config.VideoPath;
            List<SysEvent> listEvent = new List<SysEvent>();
            try
            {

               
                DataSet ds = CamercaDataAccess.SearchVideoFileName(db, eventType, camercaId, dStartTime, dEndDate);
                SysEvent osys;
               
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    osys = new SysEvent(dr);
                    listEvent.Add(osys);
                }
                return listEvent;

            }
            catch (Exception ex)
            {
                errMessage = ex.Message;
            }
            return listEvent;
        }

        public Dictionary<string, DateTime> SearchVideoFileName(int eventType, int camercaId, DateTime dStartTime, DateTime dEndDate,ref string errMessage)
        {
            Database db = DatabaseFactory.CreateDatabase();
            string baseOutpputPath = Config.VideoPath;
            Dictionary<string, DateTime> dicEvent = new Dictionary<string, DateTime>();
            try
            {

                if (eventType == 0)
                {
                    return  GeneratorFileInfo.SearchVideoFileName(Config.VideoPath, "264", camercaId, dStartTime, dEndDate);

                }
                DataSet ds = CamercaDataAccess.SearchVideoFileName(db, eventType, camercaId, dStartTime, dEndDate);
                SysEvent osys;
                Dictionary<string, DateTime> dicEvent1; 
                foreach (DataRow  dr in ds.Tables[0].Rows)
                {
                    osys = new SysEvent(dr);
                    if (!string.IsNullOrEmpty(osys.VideoFile))
                    {

                        if (!dicEvent.ContainsKey(osys.VideoFile))
                        {
                            dicEvent.Add(osys.VideoFile, DateTime.Parse(osys.EventDate));
                        }
                    }
                    //if (osys.StartDate.Length > 1)
                    //{
                    //    dicEvent1 = GeneratorFileInfo.SearchVideoFileName(Config.VideoPath, "264", osys.CamercaId, DateTime.Parse(osys.StartDate), DateTime.Parse(osys.EndDate));
                    //    foreach (KeyValuePair<string, DateTime> item in dicEvent1)
                    //    {
                    //        if (!dicEvent.ContainsKey(item.Key))
                    //        {
                    //            dicEvent.Add(item.Key, item.Value);
                    //        }

                    //    }
                    //}
                }
                return dicEvent;

            }
            catch (Exception ex)
            {
                errMessage = ex.Message;
            }
            return dicEvent;




        }
        public int Insert(ref string errMessage, CamercaInfo oCamercaInfo)
        {
            Database db = DatabaseFactory.CreateDatabase();
            errMessage = "";
            try
            {
                return CamercaDataAccess.Insert(db, oCamercaInfo);

            }
            catch (Exception ex)
            {
                errMessage = ex.Message + ex.StackTrace;
                logger.Error("Error Message:" + ex.Message + " Trace:" + ex.StackTrace);
                return -1;
            }
        }
        public int Update(ref string errMessage, CamercaInfo oCamercaInfo)
        {
            Database db = DatabaseFactory.CreateDatabase();
            errMessage = "";
            try
            {
                return CamercaDataAccess.Update(db, oCamercaInfo);

            }
            catch (Exception ex)
            {
                errMessage = ex.Message + ex.StackTrace;
                logger.Error("Error Message:" + ex.Message + " Trace:" + ex.StackTrace);
                return -1;
            }


        }
        public int Delete(ref string errMessage, int CamercaId)
        {
            Database db = DatabaseFactory.CreateDatabase();
            errMessage = "";
            try
            {
                int iRtn = CamercaDataAccess.Delete(db, CamercaId);
            
                return iRtn;
            }
            catch (Exception ex)
            {
                errMessage = ex.Message + ex.StackTrace;
                logger.Error("Error Message:" + ex.Message + " Trace:" + ex.StackTrace);
                return -1;
            }

        }

        public Dictionary<int,CamercaInfo> GetAllCamercaInfo(ref string errMessage)
        {
            Database db = DatabaseFactory.CreateDatabase();
            errMessage = "";
            Dictionary<int, CamercaInfo> list = new Dictionary<int, CamercaInfo>();
            try
            {
                
                DataSet ds = CamercaDataAccess.GetAllCamInfo(db);
                 
                CamercaInfo oCamerca;
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    oCamerca = new CamercaInfo(ds.Tables[0].Rows[i]);
                    list.Add(oCamerca.CamercaId, oCamerca);

                   
                }
                return list;

            }
            catch (Exception ex)
            {
                errMessage = ex.Message + ex.StackTrace;
                logger.Error("Error Message:" + ex.Message + " Trace:" + ex.StackTrace);
                return null;
            }
        }
        public CamercaInfo GetCamercaInfoByCamercaId(ref string errMessage, int CamercaId)
        {
            Database db = DatabaseFactory.CreateDatabase();
            errMessage = "";
            try
            {
                DataSet ds = CamercaDataAccess.GetCamInfoByCamercaId(db, CamercaId);
                if (ds.Tables[0].Rows.Count == 0)
                {
                    return null;
                }
                return new CamercaInfo(ds.Tables[0].Rows[0]);

            }
            catch (Exception ex)
            {
                errMessage = ex.Message + ex.StackTrace;
                logger.Error("Error Message:" + ex.Message + " Trace:" + ex.StackTrace);
                return null;
            }
        }
        // Create video source
     


    }
}
